package com.oro.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.oro.backingbeans.InserisciClienteBean;
import com.oro.domain.User;
import com.oro.service.UserService;

@SuppressWarnings("unused")
public class MyUserDetailsService implements UserDetailsService {

	@Autowired
	private UserService userService;

	Logger logger;

	private Logger getLogger() {
		if (logger == null) {
			logger = Logger.getLogger(InserisciClienteBean.class);
		}
		return logger;
	}

	
	public UserService getUserService() {
		return userService;
	}

	@Override
	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		User res = null;
		res = userService.getUserByUsername(username);
		if (res == null)
			throw new UsernameNotFoundException(username);
		List<GrantedAuthority> gas = res.getAuthorities();
		getLogger().info("Found user: " + res.getUsername());

		return res;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
